package LeetcodeTest_thirty_four;
//返回倒数第 k 个节点

import java.util.ArrayList;
import java.util.Scanner;

public class kthToLast
{
    public static void main(String[] args)
    {
        ListNode head=null;
        head=Solution.createListNode();
        System.out.println(Solution.kthToLast(head,2));
    }
}
class Solution
{
    public static int kthToLast(ListNode head, int k)
    {
        ListNode front,curr;
        front=curr=head;
        int distance=0;
        while(curr.next!=null)
        {
            if(distance<k-1)
            {
                curr=curr.next;
                distance++;
            }
            else
            {
                front=front.next;
                curr=curr.next;
            }
        }
        return front.val;
    }
    public static ListNode createListNode()
    {
        int a;
        Scanner reader=new Scanner(System.in);
        ListNode head=null;
        ListNode tail=null;
        head=new ListNode(0);
        tail=head;
        a=reader.nextInt();
        while(a!=-1)
        {
            ListNode p=new ListNode(a);
            tail.next=p;
            tail=tail.next;
            a=reader.nextInt();
        }
        return head.next;
    }
}
class ListNode
{
    int val;
    ListNode next;
    ListNode(int x) { val = x; }
    ListNode(int val, ListNode next) { this.val = val; this.next = next; }
}